﻿using Microsoft.AspNetCore.Mvc;
using Qy.Core;
using Qy.Scenario;
using Qy.Web.Authorize;
using System;
using System.Threading.Tasks;

namespace Qy.Web.Areas.Api;

[Route("Api/[controller]/[action]")]
[Verify]
public class UserDeptController : Base
{
    private readonly ApplicationContext _applicationContext;
    private readonly UserDeptService _UserDeptService;
    private readonly ContentCateService _contentCateService;
    private readonly DocCatService _docCatService;
    public UserDeptController(
        ApplicationContext applicationContext,
        UserDeptService adminUserDeptService,
        ContentCateService contentCateService, DocCatService docCatService)
    {
        _applicationContext = applicationContext;
        _UserDeptService = adminUserDeptService;
        _contentCateService = contentCateService;
        _docCatService = docCatService;
    }
    [HttpGet]
    [ActionPermissionFilter(Permission = PermissionKeys.UserDept_Index)]
    public async Task<IActionResult> IndexAsync()
    {
        var pagedList = await _UserDeptService.GetUserDepts();
        return Success(new { data = pagedList });
    }
    [HttpGet]
    [ActionPermissionFilter(Permission = PermissionKeys.UserDept_AddOrEditView)]
    public async Task<IActionResult> AddOrEditAsync(int id)
    {
        var deptList = await _UserDeptService.GetUserDeptListAsync();
        if (id < 1) return Success(new {deptList });
        var data = await _UserDeptService.GetInfo(id);
        return Success(new {data, deptList });
    }
    [HttpPost, ActionName("Create")]
    [ModelStateFilter]
    [ActionPermissionFilter(Permission = PermissionKeys.UserDept_AddOrEdit)]
    [Log(Name = "新增部门")]
    public async Task<IActionResult> CreatePostAsync([FromBody] EditUserDept cateModel)
    {
        cateModel.GreatTime = DateTime.Now;
        object insertPri = await _UserDeptService.Insert(cateModel);
        return Success("新增部门 ID:" + insertPri.ToString());
    }
    [HttpPut, ActionName("Edit")]
    [ModelStateFilter]
    [ActionPermissionFilter(Permission = PermissionKeys.UserDept_AddOrEdit)]
    [Log(Name = "修改部门")]
    public async Task<IActionResult> EditAsync([FromBody] EditUserDept editModel, int dyEdit = 0)
    {
        UserDept adminUserDept = await _UserDeptService.GetInfo(editModel.Id);
        if (dyEdit == 1)
        {
            //此功能前端暂时没有实现
            adminUserDept.OrderNum = editModel.OrderNum;
            if (0 < await _UserDeptService.UpdateAsync(adminUserDept))
                return Success("修改部门排序:" + adminUserDept.Name + " Id： " + editModel.Id );
            return Fail("修改部门失败:" + adminUserDept.Name + " Id： " + editModel.Id );
        }
        adminUserDept.Pid = editModel.Pid;
        adminUserDept.Name = editModel.Name;
        adminUserDept.OrderNum = editModel.OrderNum;
        adminUserDept.Remark = editModel.Remark;
        if (0 < await _UserDeptService.UpdateAsync(adminUserDept))
            return Success("修改部门:" + adminUserDept.Name + " Id： " + editModel.Id);
        return Fail("修改部门:" + adminUserDept.Name + " Id： " + editModel.Id);
    }
    [HttpDelete, ActionName("Delete")]
    [ActionPermissionFilter(Permission = PermissionKeys.UserDept_Delete)]
    [Log(Name = "删除部门")]
    public async Task<IActionResult> DeleteAsync(int id)
    {
        if (await _UserDeptService.DeleteDelCache(id))
            return Success("删除部门,Id： " + id);
        return Fail("删除失败！当前部门有用户或有下级部门！");
    }
    [HttpGet]
    [ActionPermissionFilter(Permission = PermissionKeys.UserDept_Resource)]
    public async Task<IActionResult> GetEditContentRes(int id)
    {
        if (id < 1)
            return Fail("参数错误！");
        var data = await _UserDeptService.GetInfo(id);
        var allTree = await _contentCateService.GetCatTree(await _applicationContext.ILoginUserInfoMethods.LoginUserInfoAsync());
        ContentCatRole adminUserDept = new()
        {
            Id = data.Id,
            ContentCateRole = data.ContentCateRole,
            Name = data.Name,
        };
        return Success(new {data = adminUserDept, allTree });
    }
    [HttpPut]
    [ModelStateFilter]
    [ActionPermissionFilter(Permission = PermissionKeys.UserDept_Resource)]
    [Log(Name = "网站栏目资源分配")]
    public async Task<IActionResult> PutEditContentRes([FromBody] ContentCatRole adminUserGroup)
    {
        if (0 < await _UserDeptService.UpdateContentCatRoleAsync(adminUserGroup))
            return Success("分配部门网站栏目资源 ID：" + adminUserGroup.Id);
        return Fail("分配部门网站栏目资源 ID：" + adminUserGroup.Id);
    }
    [HttpGet]
    [ActionPermissionFilter(Permission = PermissionKeys.UserDept_Resource)]
    public async Task<IActionResult> GetEditDocRes(int id)
    {
        var data = await _UserDeptService.GetInfo(id);
        var allTree = await _docCatService.GetCatTree(await _applicationContext.ILoginUserInfoMethods.LoginUserInfoAsync());
        DocCatRoles adminUserDept = new()
        {
            Id = data.Id,
            DocCatRole = data.DocCatRole,
            Name = data.Name,
        };
        return Success(new { data = adminUserDept, allTree });
    }
    [HttpPut]
    [ModelStateFilter]
    [ActionPermissionFilter(Permission = PermissionKeys.UserDept_Resource)]
    [Log(Name = "公文管理资源分配")]
    public async Task<IActionResult> PutEditDocRes([FromBody] DocCatRoles adminUserGroup)
    {
        if (0 < await _UserDeptService.UpdateDocCatRoleAsync(adminUserGroup))
            return Success("分配公文管理资源 ID：" + adminUserGroup.Id);
        return Fail("分配公文管理资源 ID：" + adminUserGroup.Id);
    }
}
